package com.yidian.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yidian.dao.ParaMapper;
import com.yidian.dao.SpecMapper;
import com.yidian.dao.TemplateMapper;
import com.yidian.entity.PageResult;
import com.yidian.pojo.goods.Para;
import com.yidian.pojo.goods.Spec;
import com.yidian.pojo.goods.Template;
import com.yidian.server.goods.TemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

/**
 * @author whb
 * @create 2021-05-17 19:33
 * @desciption 商品模板Service实现类
 */
@Service(interfaceClass = TemplateService.class)
public class TemplateServiceImpl implements TemplateService {
    @Autowired
    private TemplateMapper templateMapper;
    @Autowired
    private SpecMapper specMapper;
    @Autowired
    private ParaMapper paraMapper;

    /**
     * 返回全部记录
     * @return
     */
    @Override
    public List<Template> findAll() {
        return templateMapper.selectAll();
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页记录数
     * @return 分页查询结果
     */
    @Override
    public PageResult<Template> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        Page<Template> templates= (Page<Template>) templateMapper.selectAll();
        return new PageResult<>(templates.getTotal(),templates.getResult());
    }

    /**
     * 条件查询
     * @param searchMap 查询条件
     * @return
     */
    @Override
    public List<Template> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return templateMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     * @param searchMap 查询条件
     * @param page 页码
     * @param size 每页记录数
     * @return
     */
    @Override
    public PageResult<Template> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        Page<Template> templates= (Page<Template>) templateMapper.selectByExample(example);
        return new PageResult<>(templates.getTotal(),templates.getResult());
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @Override
    public Template findById(Integer id) {
        return templateMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增
     * @param template
     */
    @Override
    public void add(Template template) {
        template.setSpecNum(0);
        template.setParaNum(0);
        templateMapper.insertSelective(template);
    }

    /**
     * 修改
     * @param template
     */
    @Override
    public void update(Template template) {
        templateMapper.updateByPrimaryKeySelective(template);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    @Transactional
    public void delete(Integer id) {
        templateMapper.deleteByPrimaryKey(id);

        //删除该商品模板下属的规格
        Example exampleSpec=new Example(Spec.class);
        Example.Criteria criteriaSpec = exampleSpec.createCriteria();
        criteriaSpec.andEqualTo("templateId",id);
        specMapper.deleteByExample(exampleSpec);

        //删除该商品模板下属的参数
        Example examplePara=new Example(Para.class);
        Example.Criteria criteriaPara = examplePara.createCriteria();
        criteriaPara.andEqualTo("templateId",id);
        paraMapper.deleteByExample(examplePara);
    }

    /**
     * 构建查询条件
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String,Object> searchMap){
        Example example = new Example(Template.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap!=null){
            if (searchMap.get("id")!=null){
                criteria.andEqualTo("id",searchMap.get("id"));
            }

            //模板名称
            if (searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
                criteria.andLike("","%"+searchMap.get("name")+"%");
            }

            //规格数量
            if (searchMap.get("specNum")!=null){
                criteria.andEqualTo("specNum",searchMap.get("specNum"));
            }

            //参数数量
            if (searchMap.get("paraNum")!=null){
                criteria.andEqualTo("paraNum",searchMap.get("paraNum"));
            }
        }
        return example;
    }
}
